package ch.fhnw.qualiscan.application.modals;

import java.util.List;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.AdminModel;
import ch.fhnw.qualiscan.application.pages.tabs.AdminTab1;
import ch.fhnw.qualiscan.core.Modal;
import ch.fhnw.qualiscan.core.db.helpers.adminDomainTableArgs;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.DomainDescription;
import ch.fhnw.qualiscan.core.db.pojos.DomainText;
import ch.fhnw.qualiscan.core.db.pojos.Language;

import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class AdminNewDomain extends Modal {

	private Form editForm;
	private AdminModel adminModel;
	private List<Language> langList;
	private BeanItemContainer<adminDomainTableArgs> tableContainer;
	private AdminTab1 caller;
	
	public AdminNewDomain(BeanItemContainer<adminDomainTableArgs> tableContainer, AdminTab1 caller) {
		setCaption(TM.get("admin.newdomain.title"));
		this.caller = caller;
		this.tableContainer = tableContainer;
		adminModel = new AdminModel();
		langList = adminModel.getLanguages();
		buildLayout();
	}
	
	public void buildLayout() {
		editForm = new Form();
		HorizontalLayout buttonBar = new HorizontalLayout();
		editForm.getFooter().addComponent(buttonBar);
		editForm.setCaption(TM.get("admin.editdomain.title"));
		
		for(int i = 0; i < langList.size(); i++) {
			TextField titleField = new TextField();
			titleField.setWidth("100%");
			titleField.setColumns(20);
			titleField.setCaption(TM.get("admin.editdomain.domaintitle") + " " + langList.get(i).getLanguage());
			if (langList.get(i).getId() == Config.ENGLISH_ID) {
				titleField.setRequired(true);
				titleField.setRequiredError(TM.get("admin.newdomain.error"));
			}
			editForm.addField("title"+langList.get(i).getId(), titleField);
			
			TextField descriptionField = new TextField();
			descriptionField.setWidth("100%");
			descriptionField.setRows(3); 
			descriptionField.setColumns(20);
			descriptionField.setCaption(TM.get("admin.editdomain.domaindesc") + " " + langList.get(i).getLanguage());
			editForm.addField("desc"+langList.get(i).getId(), descriptionField);
		}
		
		Button saveButton = new Button(TM.get("action.save"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				try {
					editForm.commit();
					Domain d = new Domain();
					adminDomainTableArgs adta = new adminDomainTableArgs();
					adta.setDomain(d);
					adta.setNumberQuestions(0);
					for (int i = 0; i < langList.size(); i++) {
						if ((String)editForm.getField("title"+langList.get(i).getId()).getValue() != "") {
							if (langList.get(i).getId() == Config.ENGLISH_ID) {
								adta.setTitleEng((String) editForm.getField("title"+langList.get(i).getId()).getValue());
							}
							DomainText dtxt = new DomainText();
							dtxt.setLanguage(langList.get(i));
							dtxt.setText((String) editForm.getField("title"+langList.get(i).getId()).getValue());
							hibernateSession.getSession().save(dtxt);
							
							DomainDescription ddesc = new DomainDescription();
							ddesc.setLanguage(langList.get(i));
							ddesc.setText((String) editForm.getField("desc"+langList.get(i).getId()).getValue());
							hibernateSession.getSession().save(ddesc);
							
							d.addDomaintexts(dtxt);
							d.addDomaindescription(ddesc);
							
						}
					}
					hibernateSession.getSession().save(d);
					
					hibernateSession.closeSession();
					caller.admin.tabComponent2.refresh();
					tableContainer.addBean(adta);
					
					AdminNewDomain aed = (AdminNewDomain) event.getButton().getData();
					aed.close();
				} catch (InvalidValueException e) {return;}
			}
		});
		saveButton.setIcon(Config.ICON_SAVE);
		
		Button cancelButton = new Button(TM.get("action.cancel"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				AdminNewDomain aed = (AdminNewDomain) event.getButton().getData();
				aed.close();
			}
		});
		cancelButton.setIcon(Config.ICON_CANCEL);
		cancelButton.setData(this);
		
		saveButton.setData(this);
		buttonBar.addComponent(saveButton);
		
		addComponent(editForm);
		
	}
}
